package cn.edu.zstu.overlappingdisappear.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

import cn.edu.zstu.overlappingdisappear.R;
import cn.edu.zstu.overlappingdisappear.base.LocalHelper;
import cn.edu.zstu.overlappingdisappear.bean.LeverBean;
import cn.edu.zstu.overlappingdisappear.fragment.LeverFragment;

/**
 * @Author ZhuMingWei(986990501@qq.com)
 * @Date on 2019/11/8
 * @Version v1.0.0
 * @Company TODO
 * @Describle TODO
 */

public class GridViewAdapter extends BaseAdapter {

    public int start;
    public int curLevel;
    private List<LeverBean> dataList;
    public int curDifficult;

    private OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener{
        void onItemClick(ViewHolder holder, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        mOnItemClickListener = listener;
    }

    public GridViewAdapter(List<LeverBean> datas, int page) {
        dataList = new ArrayList<>();
        //start end分别代表要显示的数组在总数据List中的开始和结束位置
        int start = page * LeverFragment.item_grid_num;
        int end = start + LeverFragment.item_grid_num;
        this.start = start;
        while ((start < datas.size()) && (start < end)) {
            dataList.add(datas.get(start));
            start++;
        }
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View itemView, ViewGroup viewGroup) {
        final ViewHolder mHolder;
        if (itemView == null) {
            mHolder = new ViewHolder();
            itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_lever, viewGroup, false);
            mHolder.btnLever = itemView.findViewById(R.id.btn_lever);
            itemView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) itemView.getTag();
        }
        LeverBean bean = dataList.get(i);
        if (bean != null) {
            curLevel = LocalHelper.getCurLevel(curDifficult);
            if (bean.LEVER == curLevel){
                mHolder.btnLever.setBackgroundResource(R.mipmap.bg_btn_now);
                mHolder.btnLever.setTextColor(Color.BLACK);
            }
            else if (bean.LEVER < curLevel){
                mHolder.btnLever.setBackgroundResource(R.mipmap.bg_btn_finish);
                mHolder.btnLever.setTextColor(Color.BLACK);
            }
            else {
                mHolder.btnLever.setBackgroundResource(R.mipmap.bg_btn_unfinish);
                mHolder.btnLever.setTextColor(Color.WHITE);
            }
            mHolder.btnLever.setText(bean.LEVER+"");
        }
        if (mOnItemClickListener != null) {
            mHolder.btnLever.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = start + i;
                    mOnItemClickListener.onItemClick(mHolder, pos);
                }
            });
        }
        return itemView;
    }

    public class ViewHolder{
        private Button btnLever;
    }
}
